package service

import (
	"errors"

	"gitee.com/cyanbleeze/go-image-management/internal/model"
)

type CheckAuthRequest struct {
	Username string `json:"username" binding:"required,alphanum,min=4,max=16"`
	Password string `json:"password" binding:"required,min=6,max=24"`
}
type UserInfo struct {
	model.Model
	Username string `json:"username,omitempty"`
	Nickname string `json:"nickname,omitempty"`
}

func (svc *Service) CheckAuth(params *CheckAuthRequest) (*model.User, error) {
	user, err := svc.dao.GetValidUser(params.Username, params.Password)
	return user, err
}

func (svc *Service) GetAuth() (*UserInfo, error) {
	userID, exist := svc.ctx.Get("userID")
	if !exist {
		return nil, errors.New("登录用户信息不存在")
	}
	user, err := svc.dao.GetUser(userID.(uint))
	userInfo := &UserInfo{
		Model:    user.Model,
		Username: user.Username,
		Nickname: user.Nickname,
	}
	return userInfo, err
}
